using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAL;
using MaymuseMS.Models;

namespace MaymuseMS.Controllers {
    public class ConfigurationItemController : GenericController {

        public ActionResult Index() {
            return View();
        }

        [HttpPost]
        public ActionResult Index(JQGridParm parm, int configurationId) {
            string sidx = parm.Sidx;
            string sord = parm.Sord;
            int page = parm.Page;
            int rows = parm.Rows;

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = unitOfWork.ConfigurationItemRepository.dbSet.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var entities = unitOfWork.ConfigurationItemRepository.Get(pageIndex, pageSize, sidx, sord, parm.Filters, c => c.ConfigurationId == configurationId);

            var enums = entities.Select(e => new {
                id = e.ConfigurationItemId,
                cell = new object[] { 
                    e.ProductId,
                    e.Product.Number,
                    e.Product.Name,
                    e.Count,
                    e.Product.Unit,
                    e.Remark
                }
            });

            var jsonData = new {
                success = true,
                total = totalPages,
                page,
                records = totalRecords,
                rows = enums
            };
            return Json2(jsonData);
        }

        [HttpPost]
        public ActionResult Add(ConfigurationItem configurationitem, int configurationId) {
            ConfigurationItem configurationitemAdd = new ConfigurationItem();
            if (TryUpdateModel(configurationitemAdd)) {
                configurationitemAdd.ConfigurationId = configurationId;
                unitOfWork.ConfigurationItemRepository.Add(configurationitemAdd);
                unitOfWork.Save();
                return Json2(new { success = true });
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Edit(int? id, ConfigurationItem configurationitem) {
            if (id != null) {
                ConfigurationItem configurationitemEdit = unitOfWork.ConfigurationItemRepository.Get((int)id);
                if (configurationitemEdit != null) {
                    if (TryUpdateModel(configurationitemEdit)) {
                        unitOfWork.Save();
                        return Json2(new { success = true });
                    }
                }
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            if (string.IsNullOrWhiteSpace(id))
                Json2(new { success = false, message = errorMessage });
            try {
                unitOfWork.ConfigurationItemRepository.Remove(id);
                unitOfWork.Save();
                return Json2(new { success = true });
            } catch (ArgumentNullException e) {
                return Json2(new { success = false, message = e.Message });
            } catch (Exception e) {
                return Json2(new { success = false, message = errorMessage });
            }
        }
        public ActionResult Get(string term) {
            var jsonData = unitOfWork.ProductRepository.GetPartsSemi(term)
                .Select(p => new { id = p.Id, value = p.Name, label = p.Number + " " + p.Name });
            return Json2(jsonData);
        }
        //public ActionResult ConfigurationItems() {
        //    var configurationitems = unitOfWork.ConfigurationItemRepository.dbSet.Select(s => new { key = s.ConfigurationItemId, value = s. }).AsEnumerable();
        //    var jsonData = new { value = configurationitems };
        //    return Json2(jsonData);
        //}
    }
}

